import { useUserInfoStore } from '../pinia/userInfo.js';
import { useCacheRequest } from '../plugins/useRequest.js';
import { useTimeoutPoll } from '@vueuse/core';

export const getAutoRefreshOptions = () => {
  return {
    timeout: 3 * 60 * 1000,
    functionName: 'getDataAsync',
  };
};

export default function pollingVueRequestMixin(option) {
  return {
    data() {
      return {
        autoRefreshOptions: Object.assign({}, getAutoRefreshOptions(), option),
      };
    },
    computed: {
      params() {
        const { area_code } = useUserInfoStore();
        return { area_code: area_code };
      },
    },
    created() {
      const { timeout, functionName } = this.autoRefreshOptions;
      useTimeoutPoll(async () => {
        await useCacheRequest(this[functionName], this.params);
      }, timeout);
    },
  };
}
